﻿using System;
using System.Collections.Generic;
using System.Html;

namespace Db7
{
    public class RenderContext
    {


        public static Dictionary<string, VirtualElement> ElementLookup;

        public static int ControlCounter = 0;

        public static void RenderInsideElement(Element ElementContainer, VirtualElement VirtualContainer)
        {
            RenderContext rc = new RenderContext("tbdxxxxx");
            foreach (VirtualElement child in VirtualContainer.Children)
            {
                child.DoDefaultRender(rc, child);
            }
            ElementContainer.InnerHTML = rc.sw.ToString();
        }

        #region OnChange

        public static Dictionary<int, Action<ElementEvent>> OnChangeHandlers;

        private static bool ListeningForOnChangeEvents;

        public static void RegisterOnChangeHandler(VirtualElement matcher, Action<ElementEvent> action)
        {
            if (!ListeningForOnChangeEvents)
            {
                Document.Body.AddEventListener("change", OnChangeElementEventListener, false);
                ListeningForOnChangeEvents = true;
            }
            if (OnChangeHandlers == null) OnChangeHandlers = new Dictionary<int, Action<ElementEvent>>();
            OnChangeHandlers[matcher.GUID] = action;
        }

        private static void OnChangeElementEventListener(ElementEvent e)
        {
            Element target = e.SrcElement;
            VirtualElement ve = Queries.GetVirtualElement(target);
            if (ve == null) return;
            Action<ElementEvent> action = OnChangeHandlers[ve.GUID];
            action(e);
        }

        #endregion

        #region OnClick

        public static Dictionary<int, Action<ElementEvent>> OnClickHandlers;

        private static bool ListeningForOnClickEvents;

        public static void RegisterOnClickHandler(VirtualElement matcher, Action<ElementEvent> action)
        {
            //Script.Eval("debugger;");
            if (!ListeningForOnClickEvents)
            {
                Document.Body.AddEventListener("click", OnClickElementEventListener, false);
                ListeningForOnClickEvents = true;
            }
            if (OnClickHandlers == null) OnClickHandlers = new Dictionary<int, Action<ElementEvent>>();
            //OnClickHandlers[matcher] = action;
            OnClickHandlers[matcher.GUID] = action;
        }

        private static void OnClickElementEventListener(ElementEvent e)
        {
            Element target = e.SrcElement;
            //Script.Eval("debugger;");
            VirtualElement ve = Queries.GetVirtualElement(target);
            if (ve == null) return;
            Action<ElementEvent> action = OnClickHandlers[ve.GUID];
            if(action!=null) action(e);
        }

        #endregion

        #region OnMouseMove

        public static Dictionary<int, Action<ElementEvent>> OnMouseMoveHandlers;

        private static bool ListeningForOnMouseMoveEvents;

        public static void RegisterOnMouseMoveHandler(VirtualElement matcher, Action<ElementEvent> action)
        {
            if (!ListeningForOnMouseMoveEvents)
            {
                Document.Body.AddEventListener("mousemove", OnMouseMoveElementEventListener, false);
                ListeningForOnMouseMoveEvents = true;
            }
            if (OnMouseMoveHandlers == null) OnMouseMoveHandlers = new Dictionary<int, Action<ElementEvent>>();
            //OnClickHandlers[matcher] = action;
            OnMouseMoveHandlers[matcher.GUID] = action;
        }

        public static void UnRegisterOnMouseMoveHandler(VirtualElement matcher)
        {
            if (OnMouseMoveHandlers == null) return;
            if (!OnMouseMoveHandlers.ContainsKey(matcher.GUID)) return;
            OnMouseMoveHandlers.Remove(matcher.GUID);
        }

        private static void OnMouseMoveElementEventListener(ElementEvent e)
        {
            
            Element target = e.SrcElement;
            //Script.Eval("debugger;");
            VirtualElement ve = Queries.GetVirtualElement(target);
            if (ve == null) return;
            Action<ElementEvent> action = OnMouseMoveHandlers[ve.GUID];
            if (action != null)
            {
                action(e);
            }
        }

        #endregion


        public static int RootElementCounter = 0;

        public RenderContext(string ContainerID)
        {
            RootID = ContainerID;
            RootElement = Document.GetElementById(ContainerID);
            sw = new StringBuilder();
            Counts = new Stack<int>();
            Counts.Push(RootElementCounter);
            RootElementCounter++;
        }

        public StringBuilder sw;
        public int Count;
        public Stack<int> Counts;
        public Element RootElement;
        public string RootID;
    }
}
